/**
 * @Title: indicators_ctrl.js
 * @Description: TODO    医保费用占比
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月29日 上午8:45:59
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	
	//医疗费用
	app.controller('KPI0001Ctrl',['$scope','SettleAnalyseChartService','SettleAnalyseService',function($scope,SettleAnalyseChartService,SettleAnalyseService){
		var colors = ['#dd4814',  '#FF855A','yellow','#006397','#3ca5d0','#86d3e4','green','#aaa'];//,'#3ca5d0','#86d3e4','green'
		var colors2 = ['#dd4814','#006397','#3ca5d0','#86d3e4', '#FF855A','yellow','green','#aaa'];//,'#aaa'
		
		$scope.$watch('$parent.query.data',function(v){
			if(v){
				$scope.lineOption.xAxis= [{							
	             type : 'category',
	             boundaryGap:false,
	             data : v				        
				}];
				$scope.barOption.xAxis = [{
		             type : 'category',
		             data : v
		         }];
			}
		});
		var lineSeries = [],
		barSeries = [];
		$scope.barOption = {			
			grid:{
				y:10,
				y2:30,
				x:55
			},
			series:[],
			xAxis : [{
	             type : 'category',
	             boundaryGap: true,
	             data : $scope.$parent.query.data
	         }],
	         yAxis : [
				        {
				            type : 'value',
				            axisLabel : {
				            	 formatter: '{value}万'
				            }
				        }
				    ],
	         color:colors
		};
		
		$scope.lineOption = {
			grid:{
				y:10,
				y2:30,
				x:40
			},
			xAxis : [{
	             type : 'category',
	             boundaryGap: true,
	             data : $scope.$parent.query.data
	         }],
	         yAxis : [
				        {
				            type : 'value',
				            axisLabel : {
				                formatter: '{value}%'
				            }
				        }
			  ],
	         series:[],
	         color:colors2
		};
		/*********************************************************
		 * 
		 * Private function
		 * 
		 */
		
		function change2LegendData(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						name:'医疗费用总额',
						type:'bar',
						color:colors2[ci],
						series:seriesArr[ci++]
					},
					{
						name:'医疗费用预拔',
						type:'bar',
						color:colors2[ci],
						series:seriesArr[ci++]
					}]
				});
			});
			return resArray;
		}
		
		function change2LegendData2(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						type:'line',
						name:'医保费用占比',
						color:colors[ci],
						series:seriesArr[ci++]
					}]
				});
			});
			return resArray;
		}
		function checkedChange(arr) {
			var ci = 0, cj = 0;
			var kpiId = "KPI0001";
			SettleAnalyseChartService.getKpiChartValue(arr,kpiId,$scope.$parent.query.date).
			success(function(res){
				lineSeries = [];
				barSeries = [];
				if (res && res.data) {
					$.each(res.data, function(){
						barSeries.push({
							type:'bar',
							itemStyle: {
								normal: {
									color:colors2[cj++]
								}
							},
							name:this.name +':医疗费用总额',
							data:this.KPI0001
						},
						{
							type:'line',
							itemStyle: {
								normal: {
									color:colors2[cj++]
								}
							},
							name:this.name +':医疗费用预拨',
							data:this.KPI0003
						});
						
						lineSeries.push({
							type:'line',
							name:this.name +':医保费用占比',
							stack:this.name,
							data:this.KPI0002,
							itemStyle: {
								normal: {
										color:colors[ci++]
									}
							}
						});
					});
				}
				$scope.lineOption.series = lineSeries;
				$scope.barOption.series = barSeries;
				
				$scope.checkDatas = change2LegendData(arr, barSeries);
				$scope.checkDatas2 = change2LegendData2(arr, lineSeries);
				
				//关闭遮罩层
				SettleAnalyseService.loading = false;
			});
		}
		
		/*********************************************************
		 * 
		 * Event Handler
		 * 
		 */
		
		$scope.$on('fwaLegendGroup.legendStateChange', function(e, legendItem){
			var seriesArray, idx;
			if(legendItem.type === 'line') {
				seriesArray = $scope.lineOption.series;
			} else {
				seriesArray = $scope.barOption.series;
			}
			
			if(legendItem.hide) {
				idx = seriesArray.indexOf(legendItem.series);
				if(idx != -1) {
					seriesArray.splice(idx,1);
				} 
			} else {
				seriesArray.push(legendItem.series);
			}
		});
		
		$scope.$on('fwaLegendGroup.close',function(e, data){
			var data = SettleAnalyseService.removeFromCheckedArray({id:data.data.id});
			
			data.checked = false;
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.rowCheckChange', function(e, arr){
			checkedChange(arr);
		});
		
	}]);
	
})();